package servlet.assitant;


import dao.domain.Assistant;
import service.AssistantService;
import service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author gzb
 * @Date 2023/9/19
 */
@WebServlet(name = "AssistantRegisterServlet", value = "/AssistantRegisterServlet")
public class AssistantRegisterServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//  管理员注册
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String userType = "assistant";
        String msg = "{\"msg\":\"fail\"}";
        Assistant assistant = new Assistant(username, password);
        try {
            if (UserService.login(userType, username, password) == null) {
                AssistantService.save(assistant);
                msg = "{\"msg\":\"success\"}";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.getWriter().write(msg);
        System.out.println("finish");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println("AssistantRegisterServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
